import VueRouter from 'vue-router';
import Vue from 'vue';

import Home from '../pages/Home';
import Login from '../pages/Login';
import Register from '../pages/Register';
import Search from '../pages/Search';

Vue.use(VueRouter);

const originPush = VueRouter.prototype.push; // 先将之前的存储到一个变量当中
const originReplace = VueRouter.prototype.replace; // 同上

// 修改VueRouter原型上的push 用于解决重复跳转时报错的问题
VueRouter.prototype.push = function (location, okCallBack, errCallBack) {
    if (okCallBack === undefined && errCallBack === undefined) {
        // 上面的判断成立的话，表示没有传递这两个参数
        originPush.call(this, location).catch(() => {}); // 统一处理报错的问题 一劳永逸
    } else {
        originPush.call(this, location, okCallBack, errCallBack);
    }
};

// 修改VueRouter原型上的replace 用于解决重复跳转时报错的问题
VueRouter.prototype.replace = function (location, okCallBack, errCallBack) {
    if (okCallBack === undefined && errCallBack === undefined) {
        // 上面的判断成立的话，表示没有传递这两个参数
        originReplace.call(this, location).catch(() => {}); // 统一处理报错的问题 一劳永逸
    } else {
        originReplace.call(this, location, okCallBack, errCallBack);
    }
};

const router = new VueRouter({
    routes: [
        {
            path: '/',
            component: Home,
        },
        {
            name: 'home',
            path: '/home',
            component: Home,
            meta: {
                isShowNav: true,
            },
        },
        {
            name: 'login',
            path: '/login',
            component: Login,
            meta: {
                isHiddenFooter: true,
            },
        },
        {
            name: 'register',
            path: '/register',
            component: Register,
            meta: {
                isHiddenFooter: true,
            },
        },
        {
            name: 'search',
            path: '/search',
            component: Search,
        },
    ],
});

export default router;
